package JSPBasicGrammar.servlet04.zexercise;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TopicTest02
 */
@SuppressWarnings("all")
public class TopicTest02 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TopicTest02() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("gbk");
		PrintWriter out = response.getWriter();
		String calculator = request.getParameter("calculator");
		//out.println(calculator);
		String exrep = "\\d+\\+\\d+";
		String exrep2 = "\\d+-\\d+";
		String exrep3 = "\\d+\\*\\d+";
		String exrep4 = "\\d+/\\d+";
		if (calculator.matches(exrep)) {
			int num1 = Integer.valueOf(calculator.split("\\+", 2)[0]).intValue();
			int num2 = Integer.valueOf(calculator.split("\\+", 2)[1]).intValue();
			String count = String.valueOf(num1 + num2);
			out.println(count);
		}
		if (calculator.matches(exrep2)) {
			int num1 = Integer.valueOf(calculator.split("-", 2)[0]).intValue();
			int num2 = Integer.valueOf(calculator.split("-", 2)[1]).intValue();
			String count = String.valueOf(num1 - num2);
			out.println(count);
		}
		if (calculator.matches(exrep3)) {
			int num1 = Integer.valueOf(calculator.split("\\*", 2)[0]).intValue();
			int num2 = Integer.valueOf(calculator.split("\\*", 2)[1]).intValue();
			String count = String.valueOf(num1 * num2);
			out.println(count);
		}
		if (calculator.matches(exrep4)) {
			int num1 = Integer.valueOf(calculator.split("/", 2)[0]).intValue();
			int num2 = Integer.valueOf(calculator.split("/", 2)[1]).intValue();
			String count = String.valueOf(num1 / num2);
			out.println(count);
		}

	}

}
